package com.zsgeek.wisdchain.qutu.GestureRecognition.UI;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;
import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.view.MainActivity;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.event.PreferenceHelper;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 指纹验证
 */
public class VerifyFingerActivity extends BaseActivity {
    //    @BindView(R.id.iv_verify_finger)
//    ImageView ivVerifyFinger;
//    @BindView(R.id.tv_verify_finger)
//    TextView tvVerifyFinger;
    @BindView(R.id.tv_hand_login)
    TextView tvHandLogin;
//    @BindView(R.id.tv_mian_login)
//    TextView tvMianLogin;

    private Dialog dialog;
    private boolean isClick;
    private FingerprintIdentify mFingerprintIdentify;

    @Override
    public int getLayoutId() {
        return R.layout.activity_verify_finger;
    }

    @Override
    protected void initView() {
        mFingerprintIdentify = new FingerprintIdentify(this);
        //弹出dialog，自动弹出
        dialog = new Dialog(VerifyFingerActivity.this, R.style.Dialog);
        dialog.setContentView(R.layout.fingerprint_dialog);
        dialog.setCancelable(false);
        dialog.show();
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        if (dialog.isShowing()) {
            initVerify();
        }
        if (PreferenceHelper.getGestureFlag()) {
            tvHandLogin.setVisibility(View.VISIBLE);
        } else {
            tvHandLogin.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {

    }

    @OnClick({R.id.tv_hand_login, R.id.iv_verify_finger})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_hand_login:     //手势 等于3为认证成功
                Intent intent = new Intent(getApplicationContext(), ClosePatternPswActivity.class);
                intent.putExtra("gestureFlg", 3);
                startActivity(intent);
                mFingerprintIdentify.cancelIdentify();
                finish();
                break;
//            case R.id.tv_mian_login:
//                LogUtil.i("到自己登录页面重新登陆,处理逻辑");
//                break;
            case R.id.iv_verify_finger:
                if (!isClick) {
                    if (!dialog.isShowing()) {
                        dialog.show();
                    }
                }
                break;
        }
    }

    private void initVerify() {
        mFingerprintIdentify.startIdentify(4, new BaseFingerprint.FingerprintIdentifyListener() {
            @Override
            public void onSucceed() {
                startActivity(new Intent(VerifyFingerActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onNotMatch(int availableTimes) {
                ToastHelper.showToast(getString(R.string.verification_failed_you_still_have, availableTimes));
            }

            @Override
            public void onFailed(boolean isDeviceLocked) {
                ToastHelper.showToast(R.string.Verify_failed_fingerprint_temporarily_locked);
                isClick = true;
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            @Override
            public void onStartFailedByDeviceLocked() {
                ToastHelper.showToast(R.string.Verify_failed_fingerprint_temporarily_locked);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mFingerprintIdentify = null;
        dialog = null;
    }
}
